﻿using System.Windows;
using System.Windows.Input;
using Microsoft.Windows.Shell;

namespace WickedFlame.VecDraw.Base.Controls
{
	public class WindowExt : Window
	{
		static WindowExt()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowExt), new FrameworkPropertyMetadata(typeof(WindowExt)));
		}

		public WindowExt()
		{
			CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, OnSystemWindowCommandExecute));
			CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, OnSystemWindowCommandExecute));
			CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, OnSystemWindowCommandExecute));
			CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, OnSystemWindowCommandExecute));

			Loaded += new RoutedEventHandler(WindowExt_Loaded);

			Unloaded += new RoutedEventHandler(WindowExt_Unloaded);
		}

		void WindowExt_Unloaded(object sender, RoutedEventArgs e)
		{
			if (Owner == null)
				return;

			Owner.IsEnabled = true;
		}

		void WindowExt_Loaded(object sender, RoutedEventArgs e)
		{
			if (Owner == null)
				return;

			Owner.IsEnabled = false;
		}

		private void OnSystemWindowCommandExecute(object sender, ExecutedRoutedEventArgs e)
		{
			if (e.Command == SystemCommands.CloseWindowCommand)
				SystemCommands.CloseWindow(this);

			if (e.Command == SystemCommands.MaximizeWindowCommand)
				SystemCommands.MaximizeWindow(this);

			if (e.Command == SystemCommands.RestoreWindowCommand)
				SystemCommands.RestoreWindow(this);

			if (e.Command == SystemCommands.MinimizeWindowCommand)
				SystemCommands.MinimizeWindow(this);
		}
	}
}
